package com.atguigu.tingshu.user.api;

import com.atguigu.tingshu.common.constant.RedisConstant;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.user.UserInfo;
import com.atguigu.tingshu.user.service.UserInfoService;
import com.atguigu.tingshu.vo.user.UserInfoVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Tag(name = "用户管理接口")
@RestController
@RequestMapping("api/user/userInfo")
@SuppressWarnings({"unchecked", "rawtypes"})
public class UserInfoApiController {

	@Autowired
	private UserInfoService userInfoService;

	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 根据userId 获取到用户信息
	 * @param userId
	 * @return
	 */
	@GetMapping("/getUserInfoVo/{userId}")
	public Result<UserInfoVo> getUserInfoVo(@PathVariable Long userId){
		//	调用服务层方法.
		UserInfoVo userInfoVo = userInfoService.getUserInfo(userId);
		//	返回数据
		return Result.ok(userInfoVo);
	}

	/**
	 * 判断用户是否购买声音列表
	 * @param albumId
	 * @param trackIdList
	 * @return
	 */
	@GuiGuLogin(required = false)
	@Operation(summary = "判断用户是否购买声音列表")
	@PostMapping("userIsPaidTrack/{albumId}")
	public Result<Map<Long, Integer>> userIsPaidTrack(@PathVariable Long albumId,@RequestBody List<Long> trackIdList){
		//	获取到用户Id
		Long userId = AuthContextHolder.getUserId();
		//	调用服务层方法.
		Map<Long,Integer> map = userInfoService.userIsPaidTrack(userId,albumId,trackIdList);
		//	返回数据
		return Result.ok(map);
	}

	/**
	 * 判断用户是否购买过专辑
	 * @param albumId
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "判断用户是否购买过专辑")
	@GetMapping("/isPaidAlbum/{albumId}")
	public Result<Boolean> isPaidAlbum(@PathVariable Long albumId){
		//	获取用户Id
		Long userId = AuthContextHolder.getUserId();
		//	调用服务层方法.
		Boolean flag = userInfoService.isPaidAlbum(userId,albumId);
		//	返回数据
		return Result.ok(flag);
	}

	/**
	 * 获取专辑已支付的声音Id集合列表
	 * @param albumId
	 * @return
	 */
	@GuiGuLogin
	@Operation(summary = "获取专辑已支付的声音Id集合列表")
	@GetMapping("/findUserPaidTrackList/{albumId}")
	public Result<List<Long>> findUserPaidTrackList(@PathVariable Long albumId){
		//	获取用户Id
		Long userId = AuthContextHolder.getUserId();
		//	调用服务层方法.
		List<Long> trackIdList = userInfoService.findUserPaidTrackList(userId,albumId);
		//	返回数据
		return Result.ok(trackIdList);
	}

	/**
	 * 更新Vip到期失效状态
	 * @return
	 */
	@GetMapping("/updateVipExpireStatus")
	public Result updateVipExpireStatus(){
		//	调用服务层方法.
		userInfoService.updateVipExpireStatus();
		//	返回数据
		return Result.ok();
	}

	/**
	 * 手动获取token 数据
	 * @param userId
	 * @return
	 */
	@Operation(summary = "手动获取token")
	@GetMapping("/getToken/{userId}")
	public Result login(@PathVariable Long userId) {
		UserInfo userInfo = userInfoService.getById(userId);
		String token = UUID.randomUUID().toString().replaceAll("-", "");
		redisTemplate.opsForValue().set(RedisConstant.USER_LOGIN_KEY_PREFIX+token, userInfo, RedisConstant.USER_LOGIN_REFRESH_KEY_TIMEOUT, TimeUnit.SECONDS);
		Map<String, Object> map = new HashMap<>();
		map.put("token", token);
		return Result.ok(map);
	}
}

